#include "ArcEntity.h"
#include <TGS/GeoMath2.0/Envelope.h>
#include <TGS/MapControl2.0/RenderContext.h>
#include <QPainter>

ArcEntity::ArcEntity(void)
{
}


ArcEntity::~ArcEntity(void)
{
}

void ArcEntity::traslate( const Point& point )
{
	_center.setX(_center.getX() + point.getX());
	_center.setY(_center.getY() + point.getY());
}

void ArcEntity::setArc( const Point& center, double radius, double angel1, double angel2 )
{
	_center = center;
	_radius = radius;
	_angle1 = angel1;
	//_angle2 = angel2;
	_angle2 = 0 == angel2 ? 360 : angel2;
	/*if(angel1 > angel2)
	{
		angel1 = angel1 - 360;
	}*/
}

void ArcEntity::render( RenderContext* renderContext )
{
	renderContext->painter()->save();

	QPoint center = renderContext->mapToPixel().mapToScreen(_center);
	int radiusInPix = _radius / renderContext->mapToPixel().mapUnitsPerPixel();

	QPen pen(QBrush(getColor()), getWidth());
	renderContext->painter()->setPen(pen);

	int spanAngle = _angle2 - _angle1;
	if(_angle1 > _angle2)
	{
		spanAngle = 360 - _angle1 + _angle2;
	}
	renderContext->painter()->drawArc(center.x() - radiusInPix, center.y() - radiusInPix, 
									  radiusInPix * 2, radiusInPix * 2,
									  _angle1 * 16, spanAngle * 16);

	renderContext->painter()->restore();
}

TGS::GeoMath::Envelope ArcEntity::getEnvelope() const
{
	if(!isSpaceModel()) return Envelope();

	if(0 == _radius) return Envelope();

	Point right(_center.getX() + _radius, _center.getY());
	Point left(_center.getX() - _radius, _center.getY());
	Point top(_center.getX(), _center.getY() + _radius);
	Point down(_center.getX(), _center.getY() - _radius);

	Envelope envelope;
	envelope.expandToInclude(right);
	envelope.expandToInclude(left);
	envelope.expandToInclude(top);
	envelope.expandToInclude(down);

	return envelope;
}
